﻿using AutoMapper;
using QYmall.Core.Common.Emum;
using QYmall.Core.Common.Extensions;
using QYmall.Core.Common.Helper;
using QYmall.Core.DBFactory.Database;
using QYmall.Core.IServices;
using QYmall.Core.Model.Models;
using QYmall.Core.Model.ViewModels;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace QYmall.Core.Services
{
    /// <summary>
    /// 促销活动接口实现
    /// </summary>
    public class ShopPromotionService : BaseService, IShopPromotionService
    {
        private readonly IMapper _mapper;

        public ShopPromotionService(IDbContextFactory contentFactory, IMapper mapper) : base(contentFactory)
        {
            _mapper = mapper;
        }

        /// <summary>
        /// 查询一条记录(含优惠券及货品信息)
        /// </summary>
        public async Task<ShopPromotionDto?> QueryAsync(Expression<Func<ShopPromotion, bool>> funcWhere, WriteRoRead writeAndRead = WriteRoRead.Read)
        {
            _context = _contextFactory.CreateContext(writeAndRead);//连接数据库

            var model = await _context.Set<ShopPromotion>().FirstOrDefaultAsync(funcWhere);
            var result = _mapper.Map<ShopPromotionDto>(model);
            if (result.AwardType == 4)
            {
                var couponModel = await _context.Set<ShopCoupon>().FirstOrDefaultAsync(x => x.Id == result.AwardValue);
                if (couponModel != null)
                {
                    result.ShopCoupon = _mapper.Map<ShopCouponDto>(couponModel);
                }
            }
            if (result.AwardType == 5)
            {
                var productModel = await _context.Set<ShopGoodsProduct>()
                    .Include(x => x.ShopGoods).ThenInclude(r => r!.CategoryRelations).ThenInclude(t => t.ShopCategory)
                    .FirstOrDefaultAsync(x => x.Id == result.AwardValue);
                if (productModel != null)
                {
                    result.GoodsProduct = _mapper.Map<ShopGoodsProductListDto>(productModel);
                }
            }
            return result;
        }

        /// <summary>
        /// 查询指定数量列表
        /// </summary>
        public async Task<IEnumerable<ShopPromotion>> QueryListAsync(int top, Expression<Func<ShopPromotion, bool>> funcWhere,
            string orderBy, WriteRoRead writeAndRead = WriteRoRead.Read)
        {
            _context = _contextFactory.CreateContext(writeAndRead);//连接数据库

            var result = _context.Set<ShopPromotion>().Include(x => x.Merchant)
                .Where(funcWhere).OrderByBatch(orderBy);//调用Linq扩展类进行排序
            if (top > 0) result = result.Take(top);//等于0显示所有数据
            return await result.ToListAsync();
        }

        /// <summary>
        /// 查询分页列表
        /// </summary>
        public async Task<PaginationList<ShopPromotion>> QueryPageAsync(int pageSize, int pageIndex, Expression<Func<ShopPromotion, bool>> funcWhere,
            string orderBy, WriteRoRead writeAndRead = WriteRoRead.Read)
        {
            _context = _contextFactory.CreateContext(writeAndRead);//连接数据库

            var result = _context.Set<ShopPromotion>().Include(x => x.Merchant)
                .Where(funcWhere).OrderByBatch(orderBy);//调用Linq扩展类排序
            return await PaginationList<ShopPromotion>.CreateAsync(pageIndex, pageSize, result);
        }
    }
}
